require 'table'

source = [[
{

    VERSION 0001



    IMAGE 0x0001 ".\country_zhao_select.png" TRANSP 0x00FF00FF // 0  size: 201 x 83  palettes: 1
    IMAGE 0x0000 ".\country_zhao.png" TRANSP 0x00FF00FF // 1  size: 480 x 640  palettes: 1



    MODULES
    {
        MD	0x1000	MD_IMAGE	0	0	0	69	52
        MD	0x1001	MD_IMAGE	0	70	1	48	35
        MD	0x1002	MD_IMAGE	0	120	1	35	35
        MD	0x1003	MD_IMAGE	0	161	0	36	45
        MD	0x1004	MD_IMAGE	0	72	38	39	38
        MD	0x1005	MD_IMAGE	0	112	37	40	38
        MD	0x1006	MD_IMAGE	0	153	46	48	30
        MD	0x1007	MD_IMAGE	0	0	52	46	31
        MD	0x1020	MD_IMAGE	1	0	0	480	640
    }


    FRAME "country_zhao" // Index = 0, FModules = 1
    {
        0x2000
        FM	0x1020	0	0
    }


    FRAME "wuyang" // Index = 13, FModules = 2
    {
        0x2013
        FM	0x1020	0	0
        FM	0x1005	403	60
    }

    FRAME "taiyuan" // Index = 14, FModules = 2
    {
        0x200D
        FM	0x1020	0	0
        FM	0x1006	203	397
    }

    SPRITE_END

} // SPRITE
////////////////////////////////////////////////////////////////////////////////
]]
--print(source)

sprite = {}

sprite.version = "0000"
for v in source:gfind([[VERSION%s+(%d+)]]) do
	sprite.version = v
end

sprite.images = {}
for id, file, typ, color in source:gfind([[IMAGE%s+(%w+)%s+(".-")%s+(%a+)%s+(%w+)]]) do
	local image = {}
	image.id = id
	image.file = file
	image.type = typ
	image.color = color
	table.insert(sprite.images, image)
end

sprite.modules = {}
for mstr in source:gfind([[MODULES%s+%{(.-)%}]]) do
	for mid, idx, x, y, w, h in mstr:gfind([[MD%s+(%w+)%s+MD_IMAGE%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)]]) do
		local module = {}
		module.id = mid
		module.idx = idx
		module.x = x
		module.y = y
		module.w = w
		module.h = h

		table.insert(sprite.modules, module)
	end
end

sprite.frames = {}

for n,fstr in source:gfind([[FRAME.-"(.-)".-%{(.-)%}]]) do
	--print(n,fstr)
	local _,_, fid = fstr:find([[(0x%w+)]])
	if not fid then
		break
	end

	local frame = {}
	frame.id = fid
	frame.name = n

	for mid, posx, posy in fstr:gfind([[FM%s+(0x%w+)%s+(%d+)%s+(%d+)]]) do
		--print("##", mid, "x=", posx,"y=", posy)
		local module = {}
		module.id = mid
		module.x = posx
		module.y = posy

		table.insert(frame, module)
	end

	table.insert(sprite.frames, frame)
end

function output_images(imgs)
	for _, img in ipairs(imgs) do
		print("\tIMAGE", img.id, img.file, img.type, img.color)
	end
end

function output_modules(ms)
	print("\tMODULES")
	print("\t{")
	for _, m in ipairs(ms) do
		print("\t\tMD", m.id, "MD_IMAGE", m.idx, m.x, m.y, m.w, m.h)
	end
	print("\t}")
end

function output_sprite(s)
	print("\tVERSION", s.version)
	if s.images then
		output_images(s.images)
	else
		print("No images in sprite")
	end

	if s.modules then
		output_modules(s.modules)
	else
		print("No modules in sprite")
	end

	if s.frames then
		for _, f in ipairs(s.frames) do
			print("\tFRAME", "\""..f.name.."\"")
			print("\t{")
			print("\t",f.id)
			for _, m in ipairs(f) do
				print("\t\tFM", m.id, m.x, m.y)
			end
			print("\t}")
		end
	else
		print("No frames in sprite")
	end
end

output_sprite(sprite)
